package config;

import com.jfinal.config.*;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;
import config.interceptor.GlobalActionInterceptor;
import config.interceptor.GlobalServiceInterceptor;
import config.routes.AdminRoutes;
import config.routes.FrontRoutes;
import controller.IndexController;
import controller.LoginController;
import controller.TestController;
import controller.UserController;
import model.User;

import java.net.URL;
import java.sql.Connection;

/**
 * Created by Administrator on 2016/12/29.
 */
public class DemoConfig extends JFinalConfig{
    private Routes routes;

    public void configConstant(Constants me) {
        /*me.setEncoding("UTF-8");*/
        me.setDevMode(true);
        /*me.setBaseViewPath("/baseviewpath");*/
        me.setViewType(ViewType.JSP);
        me.setError401View("/html/401.html");
        me.setError403View("/html/403.html");
        me.setError404View("/html/404.html");
        me.setError500View("/html/500.html");

    }

    public void configRoute(Routes me) {
        this.routes = me;
        /*PropKit.use("1.txt");
        System.out.println("----");
        System.out.println("username:"+PropKit.get("username"));
        System.out.println("password:"+PropKit.get("password"));
        System.out.println("----");*/
        me.add(new FrontRoutes());
        me.add(new AdminRoutes());
        me.add("/text", TestController.class);
        me.add("/login", LoginController.class);
    }

    public void configPlugin(Plugins me) {
        C3p0Plugin cp = new C3p0Plugin("jdbc:oracle:thin:@192.168.8.233:1521:hzsmdb",
                "hzsmdb", "hzsm#DB");
        cp.setDriverClass("oracle.jdbc.driver.OracleDriver");
        me.add(cp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin("oracle",cp);
        //java.sql.SQLException: 仅 READ_COMMITTED 和 SERIALIZABLE 是有效的事务处理级
        arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
        me.add(arp);
        //配置方言
        arp.setDialect(new OracleDialect());
        //配置属性名(字段名)大小写不敏感工厂
        arp.setContainerFactory(new CaseInsensitiveContainerFactory());

        arp.addMapping("acc_user","acc_userid",User.class);

        DruidPlugin dp = new DruidPlugin("jdbc:mysql://localhost/jfinal", "root", "");
        ActiveRecordPlugin arp2 = new ActiveRecordPlugin("mysql",dp);
        me.add(dp);
        me.add(arp2);

        //缓存插件
        me.add(new EhCachePlugin("src/main/resources/ehcache.xml"));
        /*URL resource = DemoConfig.class.getClassLoader().getResource("/ehcache.xml");
        System.out.println(resource.getPath());
        me.add(new EhCachePlugin(DemoConfig.class.getClassLoader().getResource("/ehcache.xml").getFile()));*/

        //redis插件
        me.add(new RedisPlugin("news", "localhost"));
        me.add(new RedisPlugin("bbs","localhost"));


        //shiro插件
        ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
        shiroPlugin.setLoginUrl("/login/login");
        shiroPlugin.setSuccessUrl("/login/success");
        shiroPlugin.setUnauthorizedUrl("/login/unauthorized");
        me.add(shiroPlugin);

    }


    public void configInterceptor(Interceptors me) {
        //控制层拦截器  发起action请求时,调用
        //me.addGlobalActionInterceptor(new GlobalActionInterceptor());
        me.addGlobalActionInterceptor(new ShiroInterceptor());
        //服务层拦截器
        //使用Duang.duang(),Enhancer.enhance(),Controller.enhance()调用方法时,调用
        me.addGlobalServiceInterceptor(new GlobalServiceInterceptor());
    }
    public void configHandler(Handlers me) {}
}
